<template>
  <div class="tab-bar">
    <!-- <h2>tab-bar</h2> -->
    <template v-for="(item, index) in tabbarData" :key="index">
      <div class="tab-bar-item" 
      @click="itemClick(index, item)"
      :class="{ active: currentIndex === index }">
      <img v-if="currentIndex !== index" :src="getAssetURL(item.img)" alt=""></img> <!--在webpack里面可以使用 :src="require(item.img) 来读取,但是在vite里面不可以"-->
      <img v-else="currentIndex === index" :src="getAssetURL(item.activeImg)" alt=""></img> 
      <span class="text">{{ item.text }}</span>
    </div>
    </template>
  </div>
</template>

<script setup>
  import { ref } from "vue"
  import { useRouter } from "vue-router";

  import tabbarData from "@/assets/data/tabbar.js";
  import { getAssetURL } from "@/utils/loade_assets.js";
  const router = useRouter();

  const currentIndex = ref(0);
  function itemClick(index, item) {
    currentIndex.value = index;
    // console.log(item.path);
    router.push(item.path)
  }
</script>

<style lang="less" scoped>
  .tab-bar {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    height: 70px;
    display: flex;

    border-top: 1px solid #f3f3f3;
  }
  .tab-bar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    &.active {
      color: var(--primary-color);
    }

    .img {
      width: 30px;
    }

    .text {
      font-size: 11px;
      margin-top: 2px;
      margin-bottom: 2px;
    }
  }
</style>